package org.cu.ooad.finalproject.layar;

import java.util.logging.Logger;

import org.restlet.resource.Get;
import org.restlet.resource.ServerResource;

import com.google.appengine.repackaged.org.json.JSONArray;
import com.google.appengine.repackaged.org.json.JSONException;
import com.google.appengine.repackaged.org.json.JSONObject;

public class CUCampusMapLayerResource extends ServerResource {
	
	private final static Logger log = Logger.getLogger(CUCampusMapLayerResource.class.getName());

	private Map map;

	public CUCampusMapLayerResource() {
		super();
		map = new Map();
	}

	@Get
	public String getPOIList() {
		
		String radiusString = this.getQuery().getFirstValue("radius");
		String latString = this.getQuery().getFirstValue("lat");
		String lonString = this.getQuery().getFirstValue("lon");
		boolean inRange = false;
		double radius=0.0, lat=0.0, lon=0.0;
		
		try {
			lat = Double.parseDouble(latString);
			lon = Double.parseDouble(lonString);
			radius = Double.parseDouble(radiusString);
			log.info("lat: "+lat+"\tlon: "+lon+"radius: "+radius);
			inRange = true;
		} catch (Exception e) {}

		JSONObject obj = new JSONObject();
		try {
			JSONArray hotspots = null;
			if (inRange)
				hotspots = map.getPOIsInRange(lat, lon, radius);
			else 
				hotspots = map.getPOIs();
			obj.put("layer", "universityofcolorado");
			obj.put("hotspots", hotspots);
			obj.put("errorCode", 0);
			obj.put("errorString", "ok");
		} catch (JSONException e) {
			log.severe("Error while constructing response: "+e.getMessage());
		}
		return obj.toString();
	}
}
